package DataOperator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import RawDict.TagString;
import SimpleDict.DictData;

public class BaseImporter {
	static final String InputDir = "In/";
	String rawDictFile = "";
	String wordListFile = "";
	
	protected Builder builder = null;
	protected BufferedReader reader = null;
	
	public BaseImporter(String dictFile, String listFile){
		rawDictFile = dictFile;
		wordListFile = listFile;
	}
	
	protected void ImportList() throws IOException{
		if(wordListFile.isEmpty()){
			return;
		}
		builder.words.clear();
		reader = new BufferedReader(new FileReader(InputDir + wordListFile));
		String word = null;
		do{
			word = reader.readLine();
			if(word != null && !word.isEmpty()){
				builder.words.add(word);
			}
		}
		while(word != null);
	}
	
	protected void ImportDict() throws IOException{
		builder.words.clear();
		builder.dict.clear();
		reader = new BufferedReader(new FileReader(InputDir + rawDictFile));
		// import simple dict
		String s = reader.readLine();
		while(s != null){
			int i = s.indexOf(TagString.preTag);
			String word = s.substring(0, i);
			builder.words.add(word);
			WordBuildInfo info = new WordBuildInfo();
			info.data = new DictData(s);
			builder.dict.put(word, info);
			s = reader.readLine();
		}
	}
	
	protected void Import() throws IOException{
		ImportDict();
		ImportList();
	}
}
